<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // es5 构造函数
    // function Person (name, sex) {
    //   this.name = name
    //   this.sex = sex
    // }
    // Person.prototype.say = function () {
    //   console.log('我爱前端')
    // }
    // var person1 = new Person('小明', '男')
    // console.log(person1.name)
    // person1.say()

    // es6 类
    class Person {
      // 构造函数
      constructor (name, sex) {
        this.name = name
        this.sex = sex
      }
      // say () {
      //   console.log('我爱前端')
      // }
      // 静态方法
      // static say () {
      //   console.log('我爱前端')
      // }
      static getInstance (name, sex) {
        // console.log('我爱前端')
        if (!this.instance) {
          this.instance = new Person(name, sex)
        }
        return this.instance
      }
    }
    // let person1 = new Person('小明', '男')
    // console.log(person1.name)
    // console.log(person1.say === Person.prototype.say)
    // Person.say()
    // person1.say()
    let person1 = Person.getInstance('小明', '男')
    console.log(person1.name)
    let person2 = Person.getInstance('小刚', '男')
    console.log(person2.name)
  </script>
</body>
</html>